﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ConvertCsharpCode
{
    internal class CodeConverter
    {
        /// <summary>
        /// 文件路径中包含如下字符串时，不进行转换，并且删除文件
        /// </summary>
        private static readonly List<string> IgnorePath = ["\\obj\\", "\\bin\\"];

        public void ConvertCodeInDirectory(string directoryPath)
        {
            var files = Directory.GetFiles(directoryPath, "*.cs", SearchOption.AllDirectories);

            foreach (string file in files)
            {
                ConvertCodeInFile(file);
            }

            //var directories = Directory.GetDirectories(directoryPath);
            //foreach (string directory in directories)
            //{
            //    ConvertCodeInDirectory(directory);
            //}
        }

        private void ConvertCodeInFile(string filePath)
        {
            if (IgnorePath.Any(filePath.Contains))
            {
                File.Delete(filePath);
                return;
            }

            Console.WriteLine($"Converting namespace in file: {filePath}");

            string content = File.ReadAllText(filePath);

            var converters = new List<ICodeConverter>()
            {
                new NamespaceConverter(),
                new PropertyInitNewConverter(),
                new MainCtorConverter(),
                new PropertyInitConverter(),
            };

            string modifiedContent = content;
            foreach (var converter in converters)
            {
                modifiedContent = converter.Convert(modifiedContent);
            }

            if (modifiedContent != content)
            {
                File.WriteAllText(filePath, modifiedContent);
                Console.WriteLine($"Converted namespace in file: {filePath}");
            }
        }
    }

    public class NamespaceConverter : ICodeConverter
    {
        public string Convert(string content)
        {
            // namespace  xxx.xxx;
            // -> namespace  xxx.xxx { /*code*/ }
            string pattern = @"([\w\W]+?)namespace(\s*[\w\.]+?);([\w\W]+)";
            string replacement = "$1namespace $2\n{\n $3 \n}";
            return Regex.Replace(content, pattern, replacement);
        }
    }

    public class PropertyInitNewConverter : ICodeConverter
    {
        public string Convert(string content)
        {
            // public ShellViewModel ShellViewModel { get; } = new();
            // -> public ShellViewModel ShellViewModel { get; }
            string pattern2 = @"([\w\W]+?)\s*=\s*new\(\s*\);\s*";
            string replacement2 = "$1\n\n";
            return Regex.Replace(content, pattern2, replacement2);
        }
    }

    public class MainCtorConverter : ICodeConverter
    {
        public string Convert(string content)
        {
            // class MainWindowViewModel(ShellViewModel shellViewModel) : Screen
            // -> class MainWindowViewModel : Screen
            string pattern2 = @"(.*class.*?)\((.*)\)(.*)";
            string replacement2 = "$1 $3";
            return Regex.Replace(content, pattern2, replacement2);
        }
    }

    public class PropertyInitConverter : ICodeConverter
    {
        public string Convert(string content)
        {
            // public ShellViewModel ShellViewModel { get; } = shellViewModel;
            // -> public ShellViewModel ShellViewModel { get; } = shellViewModel;
            string pattern2 = @"(.*\{\s*get;.*\}\s*)=.*?;";
            string replacement2 = "$1";
            return Regex.Replace(content, pattern2, replacement2);
        }
    }
}
